package com.kede.api.stand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kede.api.stand.dto.HeadphoneDTO;
import com.kede.api.stand.service.IRecyService;
import com.kede.domain.ConfigDomain;
import com.kede.domain.RecyAreaPhoneDomain;
import com.kede.domain.RecyOrderAreaMapDomain;
import com.kede.pojo.ResHttp;
import com.kede.service.ConfigService;
import com.kede.service.RecyAreaPhoneService;
import com.kede.service.RecyOrderAreaMapService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author:Pr.li
 * @Date: create in 2023/7/1 15:18
 */
@Service
public class RecyServiceImpl implements IRecyService {

    @Resource
    private RecyAreaPhoneService recyAreaPhoneService;

    @Resource
    private ConfigService configService;

    @Resource
    private RecyOrderAreaMapService recyOrderAreaMapService;

    @Override
    public ResHttp getHeadPhoneByAdCode(Integer areaCode) {
        HeadphoneDTO headphoneDTO = new HeadphoneDTO();

        LambdaQueryWrapper<RecyAreaPhoneDomain> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RecyAreaPhoneDomain::getAreaCode,areaCode);
        RecyAreaPhoneDomain areaPhoneDomain = recyAreaPhoneService.getOne(wrapper);

        if(areaPhoneDomain != null){
            headphoneDTO.setPhone(areaPhoneDomain.getPhone());
            headphoneDTO.setPhoneName(areaPhoneDomain.getPhoneName());
            return ResHttp.okData(headphoneDTO);
        }
        //获取默认负责人
        return getDefaultHeadphoneDTO(headphoneDTO);
    }

    private ResHttp getDefaultHeadphoneDTO(HeadphoneDTO headphoneDTO) {
        LambdaQueryWrapper<ConfigDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigDomain::getName,"headphone");
        ConfigDomain one = configService.getOne(queryWrapper);
        headphoneDTO.setPhoneName("回收负责人");
        headphoneDTO.setPhone(one.getValue());
        return ResHttp.okData(headphoneDTO);
    }

    @Override
    public ResHttp getHeadPhoneByOrderId(Integer orderId) {
        RecyOrderAreaMapDomain mapDomain = recyOrderAreaMapService.getById(orderId);
        if (mapDomain != null){
            return getHeadPhoneByAdCode(mapDomain.getAreaCode());
        }
        return getDefaultHeadphoneDTO(new HeadphoneDTO());

    }


}
